package com.icbc.dayi;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class DaYi {
	public static void main(String args[]) {
		new SpellingWordFrame();
	}
	
	@Test
	public void test(){
		String date = "391211";
		try {
			Date d = new SimpleDateFormat("yyMMdd").parse(date);
			System.out.println(d);
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
			System.out.println(time);
			ArrayList<dog> list = new ArrayList<>();
			list.add(new dog("花花","女",3));
			list.add(new dog("毛毛","男",1));
			String join = StringUtils.join(list, ",");
			String string = ArrayUtils.toString(list);
			System.out.println(join);
			System.out.println(string);
		}catch (Exception e){
		
		}
	}
}

class dog{
	private String name;
	private String sex;
	private int age;
	
	public dog(String name, String sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getSex() {
		return sex;
	}
	
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "dog{" +
				"name='" + name + '\'' +
				", sex='" + sex + '\'' +
				", age=" + age +
				'}';
	}
}



class RondomString { //负责随机排列单词中的字母
	String str="";
	public String getRondomString(String s) {
		StringBuffer strBuffer=new StringBuffer(s);
		int m=strBuffer.length();
		for(int k=0;k<m;k++) {
			int index=(int)(Math.random()*strBuffer.length());//Math.random()返回(0,1)之间的随机数
			char c=strBuffer.charAt(index);
			str=str+c;
			strBuffer=strBuffer.deleteCharAt(index);
		}
		return str;
	}
}

class LetterLabel extends JTextField implements FocusListener {
	LetterLabel() {
		setEditable(false);
		this.grabFocus(); //将当前对象注册为自身的焦点视器
		setBackground(Color.white);
		setFont(new Font("Arial",Font.PLAIN,30));
	}
	public static  LetterLabel[] getLetterLabel(int n) {
		LetterLabel a[]=new LetterLabel[n];
		for(int k=0;k<a.length;k++)
			a[k]=new LetterLabel();
		return a;
	}
	public void focusGained(FocusEvent e) {
		setBackground(Color.cyan);
	}
	public void focusLost(FocusEvent e) {
		setBackground(Color.white);
	}
	public void setText(char c) {
		setText(""+c);
	}
}

class SpellingWordFrame extends JFrame implements KeyListener,ActionListener {
	JTextField inputWord;
	JButton button;
	LetterLabel label[];
	JPanel northP,centerP;
	Box wordBox;
	String hintMessage="用鼠标单击字母，按左右箭头交换字母，将其排列成所输入的单词";
	JLabel messaageLabel=new JLabel(hintMessage);
	String word="";
	SpellingWordFrame() {
		inputWord=new JTextField(12);
		button=new JButton("确定");
		button.addActionListener(this);
		inputWord.addActionListener(this);
		northP=new JPanel();
		northP.add(new JLabel("输入单词:"));
		northP.add(inputWord);
		northP.add(button);
		centerP=new JPanel();
		wordBox=Box.createHorizontalBox();
		centerP.add(wordBox);
		add(northP,BorderLayout.NORTH);
		add(centerP,BorderLayout.CENTER);
		add(messaageLabel,BorderLayout.SOUTH);
		setBounds(100,100,350,180);
		setVisible(true);
		validate();
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e) {
		word=inputWord.getText();
		int n=word.length();
		RondomString rondom=new RondomString();
		String randomWord=rondom.getRondomString(word);
		wordBox.removeAll();
		messaageLabel.setText(hintMessage);
		if(n>0) {
			label=LetterLabel.getLetterLabel(n);
			for(int k=0;k<label.length;k++) {
				label[k].setText(""+randomWord.charAt(k));
				wordBox.add(label[k]);
				label[k].addKeyListener(this); //将当前窗口注册为label[k]的键盘监视器
			}
			validate();
			inputWord.setText(null);
			label[0].requestFocus();
		}
	}
	public void keyPressed(KeyEvent e) {
		LetterLabel sourceLabel=(LetterLabel)e.getSource();
		int index=-1;
		if(e.getKeyCode()==KeyEvent.VK_LEFT) {
			for(int k=0;k<label.length;k++) {
				if(label[k]==sourceLabel) {
					index=k;
					break;
				}
			}
			if(index!=0) { //交换文本框中的字母
				String temp=label[index].getText();
				label[index].setText(label[index-1].getText());
				label[index-1].setText(temp);
				label[index-1].requestFocus();
			}
		}
		else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { //判断按下的是否是→键
			for(int k=0;k<label.length;k++) {
				if(label[k]==sourceLabel) {
					index=k;
					break;
				}
			}
			if(index!=label.length-1) {
				String temp=label[index].getText();
				label[index].setText(label[index+1].getText());
				label[index+1].setText(temp);
				label[index+1].requestFocus();
			}
		}
		validate();
	}
	public void keyTyped(KeyEvent e){}
	public void keyReleased(KeyEvent e) {
		String success="";
		for(int k=0;k<label.length;k++) {
			String str=label[k].getText();
			success=success+str;
		}
		if(success.equals(word)) {
			messaageLabel.setText("恭喜你，你成功了");
			for(int k=0;k<label.length;k++) {
				label[k].removeKeyListener(this);
				label[k].removeFocusListener(label[k]);
				label[k].setBackground(Color.white);
			}
			inputWord.requestFocus();
		}
	}
}




